#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: RT01.py 
@version:
@time: 2022/01/06 
"""
from functools import wraps

from TapClientCore.T04_logic.Dag01.node.common_clone_sink_node import CommonCloneSinkNode
from TapClientCore.T04_logic.Dag01.node.common_clone_source_node import CommonCloneSourceNode
from TapClientCore.T04_logic.Dag01.node.common_sync_sink_node import CommonSyncSinkNode
from TapClientCore.T04_logic.Dag01.node.common_sync_source_node import CommonSyncSourceNode
from TapClientCore.T04_logic.Dag01.node.sync_javascript_node import SyncSinkJavaScriptNode
from TapClientCore.T04_logic.Dag01.node.sync_sink_redis_node import SyncSinkRedisNode
from TapClientCore.T04_logic.Dag01.pipeline.pipeline01 import Pipeline01
from TapClientCore.T04_logic.Dag01.relations.relation import MapTable, Association
from TapException.TM import DataFlowBuildError


# def dag_build_log(robot):
#     @wraps(robot)
#     def wrap(**kwargs):
#         logger.info('START BUILD DAG')
#         return robot(**kwargs)
#
#     return wrap


# @dag_build_log
from TapUtil.logfunc.logger import logger


def robot_clone01(**kwargs):
    """
    robot clone 模板01，常用数据迁移报文构建
    :param kwargs:
    :return:
    """
    build_type = kwargs.get('build_type', None)
    name = kwargs.get('dataflow_name', None)
    source = kwargs.get('source', None)
    sink = kwargs.get('sink', None)
    table = kwargs.get('table', None)
    mapping = kwargs.get('mapping', None)
    setting = kwargs.get('setting', None)
    build_args = [build_type, name, source, sink, table, mapping, setting]
    for i in build_args:
        if i is None:
            logger.fatal('clone dataflows args need more')
            raise DataFlowBuildError
    S = CommonCloneSourceNode(source, table)
    T = CommonCloneSinkNode(sink)
    # 初始化对象
    line = Pipeline01(name)
    # 配置
    line.config(setting)
    # 描述
    line.readFrom(S).writeTo(T, relations=MapTable(**mapping))
    build_result = line.attr
    build_result['build_type'] = build_type
    return build_result


# @dag_build_log
def robot_sync01(**kwargs):
    """
    robot sync 模板01，常用数据迁移报文构建,
    :param kwargs:
    :return:
    """

    build_type = kwargs.get('build_type', None)
    redisKeyPrefix = kwargs.get('redisKeyPrefix', None)
    setting = kwargs.get('setting', None)
    build_args = None
    name = None
    source = None
    source_table = None
    sink = None
    redisKey = None
    sink_table = None
    association = None
    if redisKeyPrefix:
        name = kwargs.get('dataflow_name', None)
        source = kwargs.get('source', None)
        source_table = kwargs.get('source_table', None)
        sink = kwargs.get('sink', None)
        redisKey = kwargs.get('redisKey', '')
        build_args = [build_type, name, source, source_table, sink, redisKey, redisKeyPrefix, setting]
    else:
        name = kwargs.get('dataflow_name', None)
        source = kwargs.get('source', None)
        source_table = kwargs.get('source_table', None)
        sink = kwargs.get('sink', None)
        sink_table = kwargs.get('sink_table', None)
        association = kwargs.get('association', None)
        build_args = [build_type, name, source, source_table, sink, sink_table, association, setting]
    for i in build_args:
        if i is None:
            logger.fatal('clone dataflows args need more')
            raise DataFlowBuildError
    S = CommonSyncSourceNode(source, source_table)

    # sink node
    if redisKeyPrefix:
        T = ...
    else:
        T = CommonSyncSinkNode(sink, sink_table)
    line = Pipeline01(name)
    line.config(setting)
    line.readFrom(S).writeTo(T, relations=Association(**association))
    build_result = line.attr
    build_result['build_type'] = build_type
    return build_result


def robot_sync02(**kwargs):
    """
    robot sync 三个节点任务,不建议这样使用
    :param kwargs:
    :return:
    """
    build_type = kwargs.get('build_type', None)
    redisKeyPrefix = kwargs.get('redisKeyPrefix', None)
    setting = kwargs.get('setting', None)
    build_args = None
    name = None
    source = None
    source_table = None
    sink = None
    sink2 = None
    redisKey = None
    sink_table = None
    sink_table2 = None
    association = None
    if redisKeyPrefix:
        name = kwargs.get('dataflow_name', None)
        source = kwargs.get('source', None)
        source_table = kwargs.get('source_table', None)
        sink = kwargs.get('sink', None)
        redisKey = kwargs.get('redisKey', '')
        build_args = [build_type, name, source, source_table, sink, redisKey, redisKeyPrefix, setting]
    else:
        name = kwargs.get('dataflow_name', None)
        source = kwargs.get('source', None)
        source_table = kwargs.get('source_table', None)
        sink = kwargs.get('sink', None)
        sink_table = kwargs.get('sink_table', None)
        sink2 = kwargs.get('sink2', None)
        sink_table2 = kwargs.get('sink_table2', None)
        association = kwargs.get('association', None)
        build_args = [build_type, name, source, source_table, sink, sink_table, association, setting]
    for i in build_args:
        if i is None:
            logger.fatal('clone dataflows args need more')
            raise DataFlowBuildError
    S = CommonSyncSourceNode(source, source_table)

    # sink node
    if redisKeyPrefix:
        T = SyncSinkRedisNode(sink, redisKey)
    else:
        T = CommonSyncSinkNode(sink, sink_table)
    T2 = CommonSyncSinkNode(sink2, sink_table2)
    line = Pipeline01(name)
    line.config(setting)
    line.readFrom(S).writeTo(T, relations=Association(**association)).writeTo(T2, relations=Association(**association))
    build_result = line.attr
    build_result['build_type'] = build_type
    return build_result


def robot_sync03(**kwargs):
    """
    robot sync javaScript 节点，node-js node-node
    :param kwargs:
    :return:
    """
    build_type = kwargs.get('build_type', None)
    redisKey = kwargs.get('redisKey', '')
    setting = kwargs.get('setting', None)
    build_args = None
    name = None
    source = None
    source_table = None
    sink = None
    sink_table = None
    javaScriptNode = kwargs.get('javaScriptNode', None)
    javaScript = None
    association = None
    redisKeyPrefix = None
    if redisKey:
        name = kwargs.get('dataflow_name', None)
        source = kwargs.get('source', None)
        source_table = kwargs.get('source_table', None)
        sink = kwargs.get('sink', None)
        redisKeyPrefix = kwargs.get('redisKeyPrefix', None)
        build_args = [build_type, name, source, source_table, sink, redisKey, redisKeyPrefix, setting]
    else:
        name = kwargs.get('dataflow_name', None)
        source = kwargs.get('source', None)
        source_table = kwargs.get('source_table', None)
        sink = kwargs.get('sink', None)
        sink_table = kwargs.get('sink_table', None)
        javaScriptNode = kwargs.get('javaScriptNode', None)
        javaScript = kwargs.get('javaScript', None)
        association = kwargs.get('association', None)
        build_args = [build_type, name, source, source_table, sink, sink_table, association, setting, javaScriptNode,
                      javaScript]
    for i in build_args:
        if i is None:
            logger.fatal(f'sit {build_args.index(i)} clone dataflows args need more')
            raise DataFlowBuildError
    S = CommonSyncSourceNode(source, source_table)

    # js node
    JS = SyncSinkJavaScriptNode(javaScriptNode, javaScript)

    # sink
    if redisKey:
        T = SyncSinkRedisNode(sink, redisKey)
    else:
        T = CommonSyncSinkNode(sink, sink_table)
    line = Pipeline01(name)
    line.config(setting)
    line.readFrom(S).writeTo(JS).writeTo(T, relations=Association(**association))
    build_result = line.attr
    build_result['build_type'] = build_type
    return build_result
